package com.qingpeng.pay.activity;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qingpeng.pay.R;
import com.qingpeng.pay.adapter.CodeAdapter;
import com.qingpeng.pay.api.Host;
import com.qingpeng.pay.api.RedBag;
import com.qingpeng.pay.base.BaseActivity;
import com.qingpeng.pay.beans.CodeBean;
import com.qingpeng.pay.beans.NullBean;
import com.qingpeng.pay.beans.QRCodeListBean;
import com.qingpeng.pay.okhttp.RetrofitCreateHelper;
import com.qingpeng.pay.utils.MyDialog;
import com.qingpeng.pay.utils.ToastUtils;
import com.qingpeng.pay.views.TitleBar;

import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class UpQRCodeListActivity extends BaseActivity {


    @BindView(R.id.title)
    TitleBar title;
    private List<CodeBean.DataProductBean> mData;
    private MyDialog mMyDialog;
    private EditText mNumber;
    private String mType;
    private String mMoney;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_up_qrcode_list;
    }

    @Override
    protected int getRootViewId() {
        return 0;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return R.id.recycler;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mType = getIntent().getStringExtra("type");
        mMoney = getIntent().getStringExtra("money");
        title.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Call<CodeBean> qrcodelist = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).qrcodelist(mType, mMoney);
        qrcodelist.enqueue(new Callback<CodeBean>() {
            @Override
            public void onResponse(Call<CodeBean> call, Response<CodeBean> response) {
                CodeBean body = response.body();
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(UpQRCodeListActivity.this, body.getInfo());
                    return;
                }
                mData = body.getData();
                initAdapter();
            }

            @Override
            public void onFailure(Call<CodeBean> call, Throwable t) {

            }
        });
    }

    @Override
    protected void initAdapter() {
        mAdapter = new CodeAdapter(R.layout.item_qrcode_list);
        mAdapter.setNewData(mData);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, final int position) {
                final CodeBean.DataProductBean dataProductBean = mData.get(position);
                switch (view.getId()) {
                    case R.id.delete:
                        Call<NullBean> qrcodelist = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).qrcodelete(dataProductBean.getId());
                        qrcodelist.enqueue(new Callback<NullBean>() {

                            @Override
                            public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                                NullBean body = response.body();
                                if (body.getStatus() != 1) {
                                    ToastUtils.showToast(UpQRCodeListActivity.this, body.getInfo());
                                    return;
                                }
                                ToastUtils.showToast(UpQRCodeListActivity.this, body.getInfo());
                                mData.remove(position);
                                mAdapter.notifyDataSetChanged();
                            }

                            @Override
                            public void onFailure(Call<NullBean> call, Throwable t) {

                            }
                        });
                        break;
                    case R.id.text_yipao:
                        Call<QRCodeListBean> nullBeanCall = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).erweima_info(dataProductBean.getId());
                        nullBeanCall.enqueue(new Callback<QRCodeListBean>() {
                            @Override
                            public void onResponse(Call<QRCodeListBean> call, Response<QRCodeListBean> response) {
                                QRCodeListBean body = response.body();
                                if (body.getStatus() != 1) {
                                    ToastUtils.showToast(UpQRCodeListActivity.this, body.getInfo());
                                    return;
                                }
                                ToastUtils.showToast(UpQRCodeListActivity.this, "当前已跑额度: "+body.getData().getYipao()+"元");
                            }

                            @Override
                            public void onFailure(Call<QRCodeListBean> call, Throwable t) {

                            }
                        });
                        break;
                    case R.id.text_list:
                       startActivity(new Intent(UpQRCodeListActivity.this,QRCodeListMoneyActivity.class)
                       .putExtra("id",dataProductBean.getId()));
                        break;
                    case R.id.text_fenpei:
                        View view1 = getLayoutInflater().inflate(R.layout.dialog_fenpei, null);
                        mNumber = (EditText) view1.findViewById(R.id.ed_number);
                        TextView tip =(TextView) view1.findViewById(R.id.tip);
                        view1.findViewById(R.id.text_ok).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Call<NullBean> qrcodelist = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).addscore(dataProductBean.getId(),mNumber.getText().toString());
                                qrcodelist.enqueue(new Callback<NullBean>() {

                                    @Override
                                    public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                                        NullBean body = response.body();
                                        if (body.getStatus() != 1) {
                                            ToastUtils.showToast(UpQRCodeListActivity.this, body.getInfo());
                                            return;
                                        }
                                        ToastUtils.showToast(UpQRCodeListActivity.this, body.getInfo());
                                        mMyDialog.dismiss();
                                        Call<CodeBean> qrcodelist = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).qrcodelist(mType, mMoney);
                                        qrcodelist.enqueue(new Callback<CodeBean>() {
                                            @Override
                                            public void onResponse(Call<CodeBean> call, Response<CodeBean> response) {
                                                CodeBean body = response.body();
                                                if (body.getStatus() != 1) {
                                                    ToastUtils.showToast(UpQRCodeListActivity.this, body.getInfo());
                                                    return;
                                                }
                                                mData = body.getData();
                                                initAdapter();
                                            }

                                            @Override
                                            public void onFailure(Call<CodeBean> call, Throwable t) {

                                            }
                                        });
                                    }
                                    @Override
                                    public void onFailure(Call<NullBean> call, Throwable t) {

                                    }
                                });
                            }
                        });
                        view1.findViewById(R.id.text_no).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mMyDialog.dismiss();
                            }
                        });
                        mMyDialog = new MyDialog(UpQRCodeListActivity.this, 0, 0, view1, R.style.DialogTheme);
                        mMyDialog.setCancelable(true);
                        mMyDialog.show();
                        break;
                }
            }
        });
        super.initAdapter();
    }

}
